package com.sky.service.impl;

import com.sky.context.BaseContext;
import com.sky.dto.ShoppingCartDTO;
import com.sky.entity.Dish;
import com.sky.entity.Setmeal;
import com.sky.entity.ShoppingCart;
import com.sky.mapper.DishMapper;
import com.sky.mapper.SetmealMapper;
import com.sky.mapper.ShoppingCartMapper;
import com.sky.service.ShoppingCartService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;

@Service
@Slf4j
@RequiredArgsConstructor
public class ShoppingCartServiceImpl implements ShoppingCartService {
    private final ShoppingCartMapper shoppingCartMapper;
    private final DishMapper dishMapper;
    private final SetmealMapper setmealMapper;

    @Override
    public List<ShoppingCart> getShoppingCartList() {
        return shoppingCartMapper.getShoppingCartList(BaseContext.getCurrentId());
    }

    @Override
    public void addShoppingCart(ShoppingCartDTO shoppingCartDTO) {
        ShoppingCart orderDetail = new ShoppingCart();
        Integer dishCount = shoppingCartMapper.getDishCount(shoppingCartDTO.getDishId(), BaseContext.getCurrentId());
        Integer setMealCount = shoppingCartMapper.getSetmealCount(shoppingCartDTO.getSetmealId(), BaseContext.getCurrentId());
        if (dishCount > 0 && shoppingCartDTO.getDishId() != null) {
            shoppingCartMapper.updateDishNumber(shoppingCartDTO.getDishId(), BaseContext.getCurrentId());
        } else if (dishCount == 0 && shoppingCartDTO.getDishId() != null) {
            Dish dish = dishMapper.selectDishById(shoppingCartDTO.getDishId());
            orderDetail.setName(dish.getName());
            orderDetail.setAmount(dish.getPrice());
            orderDetail.setImage(dish.getImage());
            orderDetail.setUserId(BaseContext.getCurrentId());
            orderDetail.setDishId(shoppingCartDTO.getDishId());
            orderDetail.setDishFlavor(shoppingCartDTO.getDishFlavor());
            orderDetail.setCreateTime(LocalDateTime.now());
            shoppingCartMapper.addShoppingCart(orderDetail);
        }

        if (setMealCount > 0 && shoppingCartDTO.getSetmealId() != null) {
            shoppingCartMapper.updateSetmealNumber(shoppingCartDTO.getSetmealId(), BaseContext.getCurrentId());
        } else if (setMealCount == 0 && shoppingCartDTO.getSetmealId() != null) {
            Setmeal setmeal = setmealMapper.selectSetmealById(shoppingCartDTO.getSetmealId());
            orderDetail.setName(setmeal.getName());
            orderDetail.setAmount(setmeal.getPrice());
            orderDetail.setImage(setmeal.getImage());
            orderDetail.setUserId(BaseContext.getCurrentId());
            orderDetail.setSetmealId(shoppingCartDTO.getSetmealId());
            orderDetail.setDishFlavor(shoppingCartDTO.getDishFlavor());
            orderDetail.setCreateTime(LocalDateTime.now());
            shoppingCartMapper.addShoppingCart(orderDetail);
        }
    }

    @Override
    public void deleteShoppingCart() {
        shoppingCartMapper.deleteShoppingCart(BaseContext.getCurrentId());
    }

    @Override
    public void subShoppingCart(ShoppingCartDTO shoppingCartDTO) {
        Integer dishNumber = shoppingCartMapper.getDishNumber(shoppingCartDTO.getDishId(), BaseContext.getCurrentId());
        if (dishNumber != null && dishNumber > 1 && shoppingCartDTO.getDishId() != null) {
            shoppingCartMapper.updateShoppingCartByDishId(shoppingCartDTO.getDishId(), BaseContext.getCurrentId());
        } else if (dishNumber != null && dishNumber == 1 && shoppingCartDTO.getDishId() != null) {
            shoppingCartMapper.deleteShoppingCartByDishId(shoppingCartDTO.getDishId(), BaseContext.getCurrentId());
        }
        Integer setMealCount = shoppingCartMapper.getSetmealNumber(shoppingCartDTO.getSetmealId(), BaseContext.getCurrentId());
        if (setMealCount != null && setMealCount > 1 && shoppingCartDTO.getSetmealId() != null) {
            shoppingCartMapper.updateShoppingCartBySetmealId(shoppingCartDTO.getSetmealId(), BaseContext.getCurrentId());
        } else if (setMealCount != null && setMealCount == 1 && shoppingCartDTO.getSetmealId() != null) {
            shoppingCartMapper.deleteShoppingCartBySetmealId(shoppingCartDTO.getSetmealId(), BaseContext.getCurrentId());
        }
    }
}
